function changelocation(adirect){
    window.location.href = adirect;
}

function loadAjaxUpload(){
    var pageSize = $("#pageSize").val();
    var leftOrRight = $("#leftOrRight").val();
    var url = "/home/api/loadUpload?page="+pageSize;
    var serializeArray = null;
    $.post(url, serializeArray, function (result) {
        var last = result.msg;
        $("#div_last").val(last);
        var leftData = result.data[0];
        var rightData = result.data[1];
        if(leftOrRight==1){
            leftData = result.data[1];
            rightData = result.data[0];
        }
        $.each(leftData, function(i, item){
            var inner_html = loadHtmlInfo(item);
            $("#leftList").append(inner_html);

        })
        $.each(rightData, function(i, item){
            var inner_html = loadHtmlInfo(item);
            $("#rightList").append(inner_html);
        })
    });
    if(leftOrRight==0){
        $("#leftOrRight").val(1)
    }else{
        $("#leftOrRight").val(0)
    }
    $("#pageSize").val(parseInt(pageSize)+1);
}

function indexFileClick(fileId){
    var views_url = "/home/api/fileClicks/"+fileId;
    $.post(views_url,function(data,status){
        $("#_index_file"+fileId).html(data.data);
        console.log("nStatus: " + status);
    });
}

function loadHtmlInfo(_item){
    var userId = $("#userId").val();
    var objHtml = '';
    objHtml +="<div class='box pui-flex' style='text-align: center;margin-bottom:60px;'>"
    objHtml +="<div>"
    objHtml +="<img src='"+_item.url+"' width='180' class='pui-img-radius-plus-3'>"
    objHtml +="<div style='font-size:20px;'>"+_item.remark+"</div>"
    objHtml +="<div style='font-size:16px;width:80%'>"
    objHtml +="<a href='javascript:openChat("+userId+","+_item.userId+")'>"
    objHtml +="<div style='float:left'><img src='"+_item.userThum+"' style='width:32px;height:32px;' class='pui-img-circle pui-img-xs'></div>"
    objHtml +="<div style='float:left'>"+_item.userNickName+"</div>"
    objHtml +="</a>"
    objHtml +="<div style='float:right'>"
    // objHtml +="<i class='fa fa-eye fa' style='color:green;'>(1)</i>"
    objHtml +="<a href='javascript:indexFileClick("+_item.id+")'><i class='fa fa-thumbs-o-up fa'><i id='_index_file"+_item.id+"' style='padding-left:5px;'>"+_item.clicks+"</i></i></a>"
    objHtml +="</div>"
    objHtml +="</div>"
    objHtml +="</div>"
    objHtml +="</div>"
    return objHtml;
}

function openChat(fromUserId,toUserId){
    if(fromUserId===toUserId){
        alert("不能和自己私信!");
        return;
    }
    window.location.href = "/home/chats/"+fromUserId+"/"+toUserId+"";
}

$(document).on('click','.big_img',function() {
    $(this).css({
        "z-index": "-1",
        "opacity": "0"
    });
})
$(document).on('click','.pui-img-radius-plus-3',function() {
    /*调起大图 S*/
    var mySwiper = new Swiper('.swiper-container2', {
        loop: false,
        pagination: '.swiper-pagination2',
    })
    var imgBox = $("#allList").find(".pui-img-radius-plus-3");//$(this).parents(".post").find("img");
    var i = $(imgBox).index(this);
    $(".big_img .swiper-wrapper").html("")

    for(var j = 0 ,c = imgBox.length; j < c ;j++){
        var slideHtml = "<div class='swiper-slide'><div class='cell'><img src='" + imgBox.eq(j).attr('src') + "'/></div></div>";
        $(".big_img .swiper-wrapper").append(slideHtml);
    }
    mySwiper.updateSlidesSize();
    mySwiper.updatePagination();
    $(".big_img").css({
        "z-index": 1001,
        "opacity": "1"
    });
    mySwiper.slideTo(i, 0, false);
    return false;
});

$(function () {
    $("#pageSize").val(1);
    loadAjaxUpload();

    $("#wrapper1").scroll(function() {
        var last = $("#div_last").val();
        if(last == 'last'){
            return;
        }
        var scrollHeight = $("#wrapper1").scrollTop() + $("#wrapper1").height();
        var maxHeight = $("#rightList").height();
        if($("#leftList").height()>$("#rightList").height()){
            maxHeight = $("#leftList").height();
        }
        if(scrollHeight+10 > maxHeight) {
            console.log("已经滚动到底部了！");
            loadAjaxUpload();
        }
    });

    $("#photo_publish").click(function (e) {
        var autoredirect = $("#autoredirect").val();
        console.log("autoredirect"+autoredirect);
        var form = $("#publish-form");
        var url = form.attr("action");
        var serializeArray = form.serializeArray();
        $.post(url, serializeArray, function (result) {
            if(result.code !== 200){
                alert(result.msg);
            }else{
                $.dialog({
                    width: 200,
                    height: 50,
                    cache : true,
                    radius : false,
                    animated : false,
                    content : "发布成功,正在跳转中......",
                    padding: 10,
                });
                var auUrl = result.autoredirect;
                setTimeout("changelocation('"+auUrl+"')",1500 );
            }
        });
    });

    $("#upload_click").click(function (e) {
           $("#fileInput").click();
    });

    $("#fileInput").change(function (e) {
        var formData = new FormData();
        formData.append('image', $('#fileInput')[0].files[0]);
        $.ajax({
            url: '/home/upload/image',
            type: 'POST',
            data: formData,
            cache: false,
            contentType: false,
            processData: false,
            success: function(data) {
                window.location.href = "/home/upload/"+data.data.id;
            },
            error: function() {
                console.log('上传失败');
            }
        });
    });

    $("#save_user").click(function(e){
        var form = $("#user-info-form");
        var url = form.attr("action");
        var serializeArray = form.serializeArray();
        $.post(url, serializeArray, function (result) {
            alert(result.msg);
        });
    });
});